package com.dao.imp;

import com.dao.ProductDao;
import com.module.Product;
import com.utils.DruidUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class ProductDaoImpl implements ProductDao{

    JdbcTemplate jdbcTemplate=new JdbcTemplate(DruidUtil.getDataSource());

    @Override
    public List<Product> findAllProduct() {
        String sql="select * from product";
        List<Product> sql1 = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Product.class));
        return sql1;
    }

    @Override
    public Product findProductByPid(int pid) {
        String sql="select * from product where pid= ?";
        List<Product> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Product.class), pid);
        if(list !=null && list.size()== 1){
            return list.get(0);
        }
        return null;
    }
}
